﻿#include "mainwindow.h"

#include <QMessageBox>
#include <QVBoxLayout>
#include <QLabel>
#include <QFileDialog>
#include <QPrinter>
#include <QPrintDialog>
#include <QClipboard>
#include <QApplication>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),textEdit(nullptr)
{
    // 设置主窗口布局
    QVBoxLayout *layout = new QVBoxLayout;
    QLabel *label = new QLabel("按下快捷键 (Ctrl+N, Ctrl+S, Ctrl+Q, Ctrl+O, Ctrl+P, Ctrl+C, Ctrl+V)", this);
    layout->addWidget(label);

    // 添加文本编辑器
    textEdit = new QTextEdit(this);
    layout->addWidget(textEdit);

    QWidget *centralWidget = new QWidget(this);
    centralWidget->setLayout(layout);
    setCentralWidget(centralWidget);

    // 初始化快捷键
    setupShortcuts();

    // 安装事件过滤器
    this->installEventFilter(this);
}

MainWindow::~MainWindow()
{
}

bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
    if (event->type() == QEvent::KeyPress) {
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
        QKeySequence pressedKey(keyEvent->modifiers() | keyEvent->key());

        // 检查是否有对应的快捷键
        if (actionMap.contains(pressedKey.toString())) {
            QString actionName = actionMap.value(pressedKey.toString());
            executeShortcutAction(actionName);
            return true; // 拦截事件，不再继续传递
        }
    }

    // 对于未处理的事件，调用基类的事件过滤器
    return QMainWindow::eventFilter(watched, event);
}

void MainWindow::executeShortcutAction(const QString &action)
{
    if (action == "New") {
        QMessageBox::information(this, "Action Triggered", "新建一个文件");
    } else if (action == "Save") {
        QMessageBox::information(this, "Action Triggered", "保存文件");
    } else if (action == "Quit") {
        QApplication::quit();
    } else if (action == "Open") {
        QString fileName = QFileDialog::getOpenFileName(this, "打开文件", "", "Text Files (*.txt);;All Files (*)");
        if (!fileName.isEmpty()) {
            // 模拟打开文件的操作
            QMessageBox::information(this, "打开文件", "文件已打开: " + fileName);
        }
    } else if (action == "Print") {
        QPrinter printer;
        QPrintDialog printDialog(&printer, this);
        if (printDialog.exec() == QDialog::Accepted) {
            // 模拟打印操作
            QMessageBox::information(this, "打印", "Document sent to printer");
        }
    } else if (action == "Copy") {
        textEdit->copy();
        QMessageBox::information(this, "Copy", "文本已经复制");
    } else if (action == "Paste") {
        textEdit->paste();
        QMessageBox::information(this, "Paste", "文本粘贴");
    }
}

void MainWindow::setupShortcuts()
{
    // 定义动作和对应的快捷键
    shortcutMap["New"] = QKeySequence("Ctrl+N");
    shortcutMap["Save"] = QKeySequence("Ctrl+S");
    shortcutMap["Quit"] = QKeySequence("Ctrl+Q");
    shortcutMap["Open"] = QKeySequence("Ctrl+O");
    shortcutMap["Print"] = QKeySequence("Ctrl+P");
    shortcutMap["Copy"] = QKeySequence("Ctrl+C");
    shortcutMap["Paste"] = QKeySequence("Ctrl+V");

    // 定义快捷键对应的动作名称
    actionMap["Ctrl+N"] = "New";
    actionMap["Ctrl+S"] = "Save";
    actionMap["Ctrl+Q"] = "Quit";
    actionMap["Ctrl+O"] = "Open";
    actionMap["Ctrl+P"] = "Print";
    actionMap["Ctrl+C"] = "Copy";
    actionMap["Ctrl+V"] = "Paste";
}

